#!/usr/bin/env python

import wx
import os

from wx.aui import AuiPaneInfo
from utils import tracer
from uiManager import UIManager
from gui.mdiFrame import MDIFrame

class PaneManager(UIManager):  
  def __init__(self, name):
    UIManager.__init__(self, name)
    self._viewMenus = {}

  def load(self, pluginMgr):
    if pluginMgr is None:
      return

    tracer.writeln('Loading panes from plugins ...')

    self._items = {}

    plugins = pluginMgr.getPlugins()
    for plugin in plugins:
      mod = plugin.getModule()
      if mod is None:
        continue
      
      pane = mod.getPane()
      if not pane is None:
        self._items[pane.getName()] = pane

    tracer.writeln('Panes loaded. Count: %d%s' % (len(self._items), os.linesep))

  def attach(self, uiParent):
    if uiParent is None or self._items is None:
      return

    assert type(uiParent) is MDIFrame
    self._uiParent = uiParent

    tracer.writeln('%s attaching ...' % self.getName())

    auiMgr = self._uiParent.getAuiManager()
    
    tracer.writeln('Attaching panes of plugins ...')

    for pane in self._items.values():
      auiPaneInfo = self.createAuiPaneInfo(pane.getName(), pane.getCaption(), pane.getPosition(), pane.showCaption())
      auiMgr.AddPane(pane, auiPaneInfo)
      
    auiMgr.Update()

    uiParent.auiPaneCloseEventHandler(self.onPaneClose)

    tracer.writeln(' Done%s' % os.linesep)

  def detach(self):
    if self._uiParent is None or self._items is None:
      return

    tracer.writeln('%s detaching ...' % self.getName())
    tracer.writeln('Detaching panes of plugins ...')

    auiMgr = self._uiParent.getAuiManager()
    for pane in self._items.values():
      auiMgr.DetachPane(pane)

    self._uiParent = None

    tracer.writeln(' Done%s' % os.linesep)

  def createMenus(self, uiParent):
    if uiParent is None:
      return None

    panes = self.getItems()
    if not panes is None:
      menus = []

      # create file menu
      view_menu = wx.Menu()

      view_menu_panes_menu = wx.Menu()

      for pane in panes:
        if pane is None:
          continue
        view_menu_panes_menu_sub = view_menu_panes_menu.AppendCheckItem(-1, pane.getCaption())
        view_menu_panes_menu_sub.Check(pane.IsShown())
        self._viewMenus[view_menu_panes_menu_sub.GetId()] = pane
        uiParent.Bind(wx.EVT_MENU, self.onPaneMenuClicked, view_menu_panes_menu_sub)

      view_menu.AppendMenu(-1, 'Panes', view_menu_panes_menu)

      menus.append(('&View', view_menu))

      return menus

    return None

  def createAuiPaneInfo(self, name, caption, pos, showCaption):
    tracer.writeln('    - info -----------------------')
    auiPaneInfo = AuiPaneInfo()

    # set pane name
    auiPaneInfo = auiPaneInfo.Name(name)

    # set pane caption
    if showCaption:
      auiPaneInfo = auiPaneInfo.Caption(caption)
    else:
      auiPaneInfo = auiPaneInfo.CaptionVisible(showCaption)

    # set show maximize button
    auiPaneInfo = auiPaneInfo.MaximizeButton(True)

    # set show close button
    auiPaneInfo = auiPaneInfo.CloseButton(True)

    # set pane location
    if pos == wx.LEFT:
      auiPaneInfo = auiPaneInfo.Left()
    elif pos == wx.RIGHT:
      auiPaneInfo = auiPaneInfo.Right()
    elif pos == wx.TOP:
      auiPaneInfo = auiPaneInfo.Top()
    elif pos == wx.BOTTOM:
      auiPaneInfo = auiPaneInfo.Bottom()
    else:
      auiPaneInfo = auiPaneInfo.Center()

    # set pan size
    auiPaneInfo = auiPaneInfo.BestSize(wx.Size(200,200))

    return auiPaneInfo

  def onPaneClose(self, sender, args):
    caption = args.getValue()[0].GetPane().caption

    menuItem = self.getMenuItemByCaption(caption)
    if menuItem is None:
      return

    if menuItem.GetKind() == wx.ITEM_CHECK and menuItem.IsChecked():
      menuItem.Check(False)

  def onPaneMenuClicked(self, evt):
    if self._uiParent is None:
      evt.Skip()

    menuId = evt.GetId()
    if self._viewMenus.has_key(menuId):
      pane = self._viewMenus[menuId]
      auiMgr = self._uiParent.getAuiManager()
      if auiMgr is None:
        evt.Skip()
      
      auiInfo = auiMgr.GetPane(pane)
      auiInfo.Show(not auiInfo.IsShown())
      auiMgr.Update()
